package es.mllacer.qqsm;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import es.mllacer.qqsm.dialogs.CreditsDialog;
import es.mllacer.qqsm.dialogs.NoSettingsDialog;
import es.mllacer.qqsm.util.Utils;

public class Main extends FragmentActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_credits) {
			CreditsDialog.getInstance().show(getSupportFragmentManager(),
					"Creditos");
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * Start the Play activity
	 * 
	 * @param view
	 */
	public void playAction(View view) {
		SharedPreferences prefs = getSettingsPreferences();
		String user = prefs.getString(Utils.USER_PREFERENCE, "");

		if (user == null || "".equals(user)) {
			NoSettingsDialog.getInstance().show(getSupportFragmentManager(),
					"No Settings Dialog");
		} else {
			startActivity(new Intent(this, Play.class));
		}
	}

	/**
	 * Starts the Scores activity
	 * 
	 * @param view
	 */
	public void scoresAction(View view) {
		startActivity(new Intent(this, Scores.class));
	}

	/**
	 * Starts the Settings activity
	 * 
	 * @param view
	 */
	public void settingsAction(View view) {
		startActivity(new Intent(this, Settings.class));
	}

	private SharedPreferences sharedPreferences = null;

	private SharedPreferences getSettingsPreferences() {
		if (sharedPreferences == null) {
			sharedPreferences = getSharedPreferences(
					Utils.SETTINGS_PREFERENCES, MODE_PRIVATE);
		}
		return sharedPreferences;
	}

}
